package httpclient;

import common.pojo.CommonResult;
import common.utils.JsonUtils;
import httpclient.bean.HttpResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class HttpClientUtils {

    static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    /**
     * 获取HttpClient
     */
    static HttpClient httpClient = HttpClient.getInstance()
            .newBuilder().build();

    public static String getBodyStr(String url){
        HttpResult<String> httpResult = httpClient.new Request(){}.get().request(url);
        return httpResult.getBodyStr();
    }

    public static String getBodyStr(String url, Map<String,Object> params){
        HttpResult<String> httpResult = httpClient.new Request(){}.setUrlArgs(params).get().request(url);
        return httpResult.getBodyStr();
    }

    public static HttpResult getHttpResult(String url){
        HttpResult<String> httpResult = httpClient.new Request(){}.get().request(url);
        return httpResult;
    }

    public static CommonResult get(String url){
        HttpResult<String> httpResult = httpClient.new Request(){}.get().request(url);
        return wrapperHttpResult(httpResult);
    }

    public static CommonResult get(String url, Map<String,Object> params){
        HttpResult<String> httpResult = httpClient.new Request(){}.setUrlArgs(params).get().request(url);
        return wrapperHttpResult(httpResult);
    }

    public static CommonResult post(String url, Map<String,Object> params){
        HttpResult<String> httpResult = httpClient.new Request(){}.setBody(params).post().request(url);
        return wrapperHttpResult(httpResult);
    }

    public static HttpResult postW(String url, String json){
        HttpResult httpResult =  httpClient.new Request(){}.setBody(json).post().request(url);
        return httpResult;
    }

    public static CommonResult put(String url){
        HttpResult<String> httpResult = httpClient.new Request(){}.put().request(url);
        return wrapperHttpResult(httpResult);
    }

    public static CommonResult put(String url, Map<String,Object> params){
        HttpResult<String> httpResult = httpClient.new Request(){}.setBody(params).put().request(url);
        return wrapperHttpResult(httpResult);
    }

    public static CommonResult delete(String url, Map<String,Object> params){
        HttpResult<String> httpResult = httpClient.new Request(){}.setUrlArgs(params).delete().request(url);
        return wrapperHttpResult(httpResult);
    }

    private static CommonResult wrapperHttpResult(HttpResult<String> result) {
        CommonResult crt = null;
        if (result==null) {
            crt = new CommonResult();
            crt.setCode(-1);
            crt.setMessage("服务模块无法访问");
            return crt;
        }
        if(StringUtils.isNotBlank(result.getError())){
            crt = new CommonResult();
            crt.setCode(-1);
            crt.setMessage(result.getError());
            return crt;
        }
        crt = JsonUtils.parseJson(result.getResult(), CommonResult.class);
        if (crt == null) {
            crt = new CommonResult();
            crt.setCode(-1);
            crt.setMessage("服务模块返回数据格式错误");
            return crt;
        }
        return crt;
    }

}
